# apps/zhihu/strategies.py
from utils.base_strategies import DeleteStrategy, BaseDataQueryStrategy
from .models import XhsNote, XhsNoteComment

class XhsDeleteStrategy(DeleteStrategy):
    def delete_content(self, task_id):
        XhsNote.objects.filter(task_id=task_id).delete()

    def delete_comments(self, task_id):
        note_id_ids = XhsNote.objects.filter(task_id=task_id).values_list('note_id', flat=True)
        XhsNoteComment.objects.filter(note_id__in=note_id_ids).delete()


class XhsDataQueryStrategy(BaseDataQueryStrategy):
    """小红书数据查询策略类"""

    # 可以根据需要覆盖时间戳字段列表
    timestamp_fields = ['time', 'last_update_time', 'add_ts']

    def get_platform_data(self, task_id: str, **kwargs):
        """获取小红书平台数据"""
        try:
            # 根据爬虫配置获取小红书数据
            xhs_data = XhsNote.objects.filter(
                task_id=task_id
            ).values(
                'note_id',
                'title',
                'desc',
                'video_url',
                'liked_count',
                'collected_count',
                'comment_count',
                'share_count',
                'time',
                'last_update_time',
                'add_ts'
            )
            
            # 处理时间戳字段
            processed_data = self.process_timestamp_fields(list(xhs_data))
            
            return {
                'code': 0,
                'data': processed_data,
                'message': 'success'
            }
            
        except Exception as e:
            return {
                'code': 500,
                'message': f'获取小红书数据失败: {str(e)}'
            } 